﻿using Fungus;
using strange.extensions.context.api;
using strange.extensions.dispatcher.eventdispatcher.api;
using strange.extensions.mediation.impl;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
namespace CommonIoC
{
    public class FlowchartMediator : Mediator
    {

        [Inject]
        public FlowchartView view { get; set; }

        [Inject(ContextKeys.CONTEXT_DISPATCHER)]//全局的dispatcher
        public IEventDispatcher dispatcher { get; set; }

        /// <summary>
        /// 这里做初始化的操作，相当于start
        /// </summary>
        public override void OnRegister()
        {
            //view.buttonClicked.AddListener(doManagement.Dispatch);
            view.flowchart = view.GetComponent<Flowchart>();

            view.dispatcher.AddListener(MainUIEvent.CloseAllUI, onViewCallCloseAllUI);

            view.dispatcher.AddListener(PlayerGameState.IsDoingTalk , onViewCallDoingTalk );

            view.dispatcher.AddListener(PlayerGameState.CanMove_And_CanTouchWSAD, onViewCallCanMove_And_CanPressEsc);

            view.dispatcher.AddListener(FlowchartEvent.Talk_TO_RedLangOne, onTalk_TO_RedLangOne);
            view.dispatcher.AddListener(FlowchartEvent.Talk_TO_RedLangTwo, onTalk_TO_RedLangTwo);
            view.dispatcher.AddListener(FlowchartEvent.TalkToGet_1_Tank   , onTalkToGet_1_Tank );


            dispatcher.AddListener(FlowchartEvent.CHECK_TO_Talk, OnCheck_To_Talk);

            dispatcher.AddListener(FlowchartEvent.CHECK_TO_Search, OnCheck_To_Search);

            CheckIsTalkToBaBa();
        }

        private void onTalkToGet_1_Tank(IEvent payload)
        {

            Debug.Log("onTalkToGet_1_Tank启动.......");
            view.flowchart.SendFungusMessage("发现1号战车");
            GameObject.Find("PlayerManagerView").GetComponent<PlayerStageManagerView>().TriggerJoinBattleScene();
           // CommonHelper.SetPlayerTouchZero();


        }

        private void onTalk_TO_RedLangTwo(IEvent payload)
        {
            throw new NotImplementedException();
        }

        private void onTalk_TO_RedLangOne(IEvent payload)
        {
            view.flowchart.SendFungusMessage("与战狗战斗后红狼提问是否一个人来");
        }

        private void onViewCallCanMove_And_CanPressEsc(IEvent payload)
        {
            dispatcher.Dispatch(PlayerGameState.CanMove_And_CanTouchWSAD);
        }

        private void onViewCallDoingTalk(IEvent payload)
        {
            dispatcher.Dispatch(PlayerGameState.IsDoingTalk);
        }

        void CheckIsTalkToBaBa()
        {
           bool result = GameDataManager.Instance.GetTalkValueByKey("拉多村2楼与爸爸对话");
            Debug.Log("对话结果是：" + result);
            if(result == false)
            {
                view.flowchart.SendFungusMessage("执行与爸爸初始对话");

                dispatcher.Dispatch(PlayerGameState.IsDoingTalk);
            }

        }


        private void onViewCallCloseAllUI(IEvent payload)
        {
            dispatcher.Dispatch(MainUIEvent.CloseAllUI);
        }

        private void OnCheck_To_Search(IEvent payload)
        {
            view.flowchart.SendFungusMessage("调查周围有没有物品");


        }

        private void OnCheck_To_Talk(IEvent payload)
        {
            // throw new NotImplementedException();
            view.flowchart.SendFungusMessage("查找周围有没有可谈话对象");


        }



    }
}
